//
//  AdvertisementView.m
//  TestTableView
//
//  Created by Hunk on 11-12-22.
//  Copyright (c) 2011年 __MyCompanyName__. All rights reserved.
//

#import "AdvertisementView.h"

@interface AdvertisementView (Private)

- (void)startTimer;

- (void)stopTimer;

@end

@implementation AdvertisementView
@synthesize delegate = _delegate;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) 
    {
        advertisementArray = [[NSMutableArray alloc] init];
        
        // Init scrollView
        _scrollView = [[UIScrollView alloc] initWithFrame:self.bounds];
        [_scrollView setBackgroundColor:[UIColor clearColor]];
        [_scrollView setDelegate:self];
        [_scrollView setPagingEnabled:YES];
        [_scrollView setShowsHorizontalScrollIndicator:NO];
        [_scrollView setShowsVerticalScrollIndicator:NO];
        [self addSubview:_scrollView];
    }
    return self;
}

- (void)setAdvertisementData:(NSArray *)dataArray
{
    [advertisementArray addObjectsFromArray:dataArray];
    
    [_scrollView setContentSize:CGSizeMake(self.bounds.size.width * [dataArray count], self.bounds.size.height)];
    
    for(int i = 0; i < [dataArray count]; i++)
    {
        UIButton* adImageButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [adImageButton setFrame:CGRectMake(i * self.bounds.size.width, self.bounds.origin.y, self.bounds.size.width, self.bounds.size.height)];
        [adImageButton setBackgroundColor:[UIColor clearColor]];
        [adImageButton setBackgroundImage:[UIImage imageNamed:[dataArray objectAtIndex:i]]
                                 forState:UIControlStateNormal];
        [adImageButton setBackgroundImage:[UIImage imageNamed:[dataArray objectAtIndex:i]]
                                 forState:UIControlStateHighlighted];
        [adImageButton setTag:i];
        [adImageButton addTarget:self action:@selector(adButtonHandle:) forControlEvents:UIControlEventTouchUpInside];
        [_scrollView addSubview:adImageButton];
    }
    
    [self startTimer];
}

#pragma mark -
#pragma mark UIScrollView delgate methods
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    [self stopTimer];
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{   
    if([_delegate respondsToSelector:@selector(advertisementView:currentAdvertisement:)])
    {
        [_delegate advertisementView:self currentAdvertisement:(_scrollView.contentOffset.x / _scrollView.bounds.size.width)];
    }
    
    [self startTimer];
}

#pragma mark -
#pragma mark Private methods
- (void)startTimer
{   
    _timer = [[NSTimer timerWithTimeInterval:2.0 target:self selector:@selector(timerHandle:) userInfo:nil repeats:YES] retain];
    
    [[NSRunLoop currentRunLoop] addTimer:_timer forMode:NSDefaultRunLoopMode];
}

- (void)stopTimer
{
    if(_timer.isValid)
    {
        [_timer invalidate];
        
        _timer = nil;
        [_timer release];
    }   
}

- (void)timerHandle:(NSTimer *)timer
{
    CGFloat offSet_X = _scrollView.contentOffset.x + _scrollView.frame.size.width;
    
    if(offSet_X == _scrollView.contentSize.width)
    {
        offSet_X = 0.0;
    }
    
    [_scrollView setContentOffset:CGPointMake(offSet_X, _scrollView.frame.origin.y) animated:YES];
    
    if([_delegate respondsToSelector:@selector(advertisementView:currentAdvertisement:)])
    {
        [_delegate advertisementView:self currentAdvertisement:(offSet_X / _scrollView.bounds.size.width)];
    }
}

#pragma mark -
#pragma mark adButtonHandle
- (void)adButtonHandle:(id)sender
{
    UIButton* button = (UIButton*)sender;
    
    if([_delegate respondsToSelector:@selector(advertisementView:didSelectAdvertisementAtIndex:)])
    {
        [_delegate advertisementView:self didSelectAdvertisementAtIndex:button.tag];
    }
}

- (void)dealloc
{
    _scrollView = nil;
    [_scrollView release];
    
    advertisementArray = nil;
    [advertisementArray release];
    
    [super dealloc];
}

@end
